package com.stream.service.impl;

import com.google.common.collect.Maps;
import com.stream.model.entity.ScheduleJobEntity;
import com.stream.repository.ScheduleJobRepository;
import com.stream.service.ScheduleJobService;
import com.stream.utils.Constant;
import com.stream.utils.schedule.ScheduleUtils;
import org.quartz.CronTrigger;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yebaokang on 2017/6/30.
 */
@Service("scheduleJobService")
public class ScheduleJobServiceImpl implements ScheduleJobService {

    @Autowired
    private Scheduler scheduler;

    @SuppressWarnings("SpringJavaAutowiringInspection")
    @Autowired
    private ScheduleJobRepository scheduleJobRepository;

    /**
     * 项目启动时，初始化定时器
     */
    @PostConstruct
    public void init(){
        List<ScheduleJobEntity> list = scheduleJobRepository.queryList(new HashMap<>());
        for (ScheduleJobEntity job : list ) {
            CronTrigger cronTrigger = ScheduleUtils.getCronTrigger(scheduler, job.getJobId());
            //如果不存在，则创建
            if (cronTrigger == null) {
                ScheduleUtils.createScheduleJob(scheduler, job);
            } else {
                ScheduleUtils.updateScheduleJob(scheduler, job);
            }
        }

    }

    @Override
    public ScheduleJobEntity queryObject(Long jobId) {
        return scheduleJobRepository.queryObject(jobId);
    }

    @Override
    public List<ScheduleJobEntity> queryList(Map<String, Object> map) {
        return scheduleJobRepository.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return scheduleJobRepository.queryTotal(map);
    }

    @Override
    public void save(ScheduleJobEntity job) {
        job.setCreateTime(new Date());
        job.setStatus(Constant.ScheduleStatus.NORMAL.getValue());
        scheduleJobRepository.save(job);

        ScheduleUtils.createScheduleJob(scheduler, job);
    }

    @Override
    public void update(ScheduleJobEntity job) {
        scheduleJobRepository.update(job);

        ScheduleUtils.updateScheduleJob(scheduler, job);
    }

    @Override
    public void deleteBatch(Long[] jobIds) {
        scheduleJobRepository.deleteBatch(jobIds);

        for (Long jobId : jobIds) {
            ScheduleUtils.delete(scheduler, jobId);
        }

    }

    @Override
    public int updateBatch(Long[] jobIds, int status) {
        HashMap<String, Object> map = Maps.newHashMap();
        map.put("list", jobIds);
        map.put("status", status);
        return scheduleJobRepository.updateBatch(map);
    }

    @Override
    @Transactional
    public void run(Long[] jobIds) {
        for (Long jobId : jobIds) {
            ScheduleUtils.run(scheduler, queryObject(jobId));
        }
    }

    @Override
    @Transactional
    public void pause(Long[] jobIds) {
        for (Long jobId : jobIds) {
            ScheduleUtils.pause(scheduler, jobId);
        }

        updateBatch(jobIds, Constant.ScheduleStatus.PAUSE.getValue());
    }

    @Override
    @Transactional
    public void resume(Long[] jobIds) {
        for (Long jobId : jobIds) {
            ScheduleUtils.resume(scheduler, jobId);
        }

        updateBatch(jobIds, Constant.ScheduleStatus.NORMAL.getValue());
    }
}
